/******************************************************************
NOTES: 
This do file is based on the do file "universe_urban_share1_v2.do". 
It contains all the trend figures. JP (2019-9-22) ;



*******************************************************************/

# delim ;  
clear all;
set matsize 11000 ;
eststo clear ;
capture program drop add_lab_FE;
capture log close ; 
log using "../logs/Trends_figures.log", text replace;


* THIS IS THE SCHEME FOR THE GRAPHS. ALL THE GRPHS WILL HAVE THE SAME SCHEME;


*settings some settings that are common for the set of plots on this dofile; 

local pdf_plot_settings = 	`"
				xmtick(1984(1)2008)
				xlabel(1984(4)2008)
				ylabel(#3,)
				ymticks(##4)
				scale(*1.5) 
				xtitle("")
				ytitle("")
				legend(off)"';
*scale(*1.3): This rescales the font size of the whole figure;	
********************************************************************;

	
use "../data/state_year_all_80_15.dta", clear  ;


*---------------------------------------------------;
* - Generate variables and general cleanings ;
*---------------------------------------------------;
do "_setup_expenditure.do";
do "_cleaning_and_new_variables.do";

xtset state year;
gen Dmiles=d.u_miles_IH ;

gen no_exp=cond(exp_L_IH_SF12a_r==. | exp_L_IH_SF12a_r==. | exp_L_IH_SF12a_r<0,1,0);


*Defining macro variables; 
global lm_var u_lane_miles_IH u_lane_miles_rural u_lane_miles_urban u_miles_IH
				 s_lane_miles_IH s_lane_miles_rural_IH s_lane_miles_urban_IH s_lane_miles_rig_IH
				 ;

				 
gen lm_IH_h2O= 		u_lane_miles_IH*sh_h20_10m_s; 
gen lm_IH_urb= 		u_lane_miles_IH*sh_urb_10m_s; 
gen lm_IH_elev= 	u_lane_miles_IH*sh_elev_10m_s; 
gen lm_IH_sd_elev=	u_lane_miles_IH*sh_sd_elev_10m_s ;
gen lm_IH_temp= 	u_lane_miles_IH*sh_temp_10m_s; 
gen lm_IH_precip= 	u_lane_miles_IH*sh_precip_10m_s; 
gen lm_IH_sh_new_lm  = 	u_lane_miles_IH*sh_new_lm;
gen lm_IH_unionization= 	u_lane_miles_IH*unionization;
gen lm_IHunionization_Mem_Priv_Cons= 	u_lane_miles_IH*unionization_Cov_Priv_Cons ;
gen lm_IHunionization_Mem_Total= 	u_lane_miles_IH*unionization_Cov_Total ;


gen lm_IH_grade = u_lane_miles_IH*s_average_grade_seg_IH ;
format lm_IH_grade  %3.2fc;
gen lm_IH_new=Du_lane_miles_IH*sh_new_lm;
*replace lm_IH_new=. if year==1984;

global comp_var lm_IH_h2O    lm_IH_urb    lm_IH_elev    lm_IH_temp    lm_IH_precip lm_IH_sd_elev lm_IH_grade lm_IH_new  Dmiles
				sh_h20_10m_s sh_urb_10m_s sh_elev_10m_s sh_temp_10m_s sh_precip_10m_s sh_sd_elev_10m_s 
				lm_IH_unionization  lm_IHunionization_Mem_Priv_Cons  lm_IHunionization_Mem_Total;

gen u_inches_IH=	u_iri_IH*u_lane_miles_IH;
gen u_inches_urban=	u_iri_urban*u_lane_miles_urban;
gen u_inches_rural=	u_iri_rural*u_lane_miles_rural;

gen s_inches_IH=	s_iri_IH*s_lane_miles_IH;
drop u_iri_IH s_iri_IH s_trucks_av_IH;

global exp_var   exp_L_IH_SF12a_r exp_IRI_IH_SF12a_r exp_IH_mtn_SF12a_r exp_IH_mtn_SF12_r exp_row_IH_all
				 exp_relocation_IH_all_r 
				s_vmt_IH u_vmt_IH s_vmt_trucks_av_IH s_vmt_trucks_peak_IH  u_inches_IH s_inches_IH u_inches_rural u_inches_urban
				s_vmt_NHS u_vmt_rural u_vmt_urban;	

global rev_var rev_grand_total_mt_r rev_total_h_r rev_total_mt_r rev_total_m_fuel_h_r;
*---------------------------------------------------;
* - Collapse (year) ;
*---------------------------------------------------;
drop if year<1984;
*replace 

*u_lane_miles_IH
*conversion factor to get to 10^6 2010 dollars;
		
sort year;
collapse (mean) asphalt_price Surf_BituminousconcreteAv_ctn10
 (sum) ${lm_var}
			   ${comp_var} 
			   ${exp_var}
			   ${rev_var}
		     , by(year);


*Change in miles as a share of total miles; 
gen sh_Dmiles= Dmiles/u_miles_IH;
replace sh_Dmiles=. if year ==1980;




gen Bituminousconcrete_plm=(Surf_BituminousconcreteAv_ctn10*392/1000000) if year>1990;

gen Bituminousconcrete_plm_inv=-Bituminousconcrete_plm^-1 if year>1989;

/*
Since an average lane of interstate is 12 feet wide, resurfacing one lane mile
requires about 196 cubic yards of asphaltic concrete. At about two tons per cubic yard, this is 392
tons of paving material.

*/

preserve ;
keep Bituminousconcrete_plm_inv Bituminousconcrete_plm year ;
save  "../intermediate_data/Bituminousconcrete_plm_data", replace;
restore ;


twoway (scatter Bituminousconcrete_plm_inv year if year>1989, color(black) msize(*1) m(D)  lw(*2)),
	ytitle("")
	xtitle(1990(10)2010)
xmtick(1990(1)2008)
ylabel(-100(50)0)
				
				ymticks(##5)
				ymticks(##5)
				scale(*1.5) 
				xtitle("")
				legend(off)
				xtitle("")
	leg(off); 
	graph export "${output}/figures/Figure4_Bituminousconcrete_plm_inv.pdf", replace;




gen unionization= 	lm_IHunionization_Mem_Priv_Cons/u_lane_miles_IH;
gen unionization_total= 	lm_IHunionization_Mem_Total/u_lane_miles_IH;
format  unionization %3.0fc;
*Generate agg variables;

foreach x in s u{;
	gen `x'_urban_share_IH= `x'_lane_miles_urban/`x'_lane_miles_IH;
	
};

 gen s_rig_share_IH=s_lane_miles_rig_IH/s_lane_miles_IH ;
 
foreach x in h2O urb elev sd_elev temp precip grade new{;
	gen IH_share_`x'= 	lm_IH_`x'/u_lane_miles_IH;
	
};


format IH_share_grade %3.1fc;
format IH_share_h2O %5.3fc;
format IH_share_urb %5.3fc;
format IH_share_new  %5.3fc;


replace IH_share_new=. if year==1980 ;

*recode zeros to missings;
foreach varname in exp_L_IH_SF12a_r exp_IRI_IH_SF12a_r exp_IH_mtn_SF12a_r exp_relocation_IH_all_r u_lane_miles_IH s_lane_miles_IH s_vmt_IH u_vmt_IH s_vmt_trucks_av_IH s_vmt_trucks_peak_IH u_inches_IH s_inches_IH{;	
	replace `varname'=cond(`varname'==0,.,`varname');
	};
	
replace IH_share_grade=. if year<1992;	


gen u_aadt_IH=		(u_vmt_IH/u_lane_miles_IH)/365;


gen u_aadt_IH_rural=		(u_vmt_rural/u_lane_miles_rural)/365;
gen u_aadt_IH_urban=		(u_vmt_urban/u_lane_miles_urban)/365;


gen s_aadt_IH=		(s_vmt_IH/s_lane_miles_IH)/365;

gen s_aadt_trucks_av_IH=	s_vmt_trucks_av_IH /s_lane_miles_IH;
gen s_aadt_trucks_peak_IH=	s_vmt_trucks_peak_IH/s_lane_miles_IH;
gen s_iri_IHnat = 	s_inches_IH/s_lane_miles_IH;
replace s_iri_IHnat=. if year<1992;
gen u_iri_IHnat = 	u_inches_IH/u_lane_miles_IH;
gen u_iri_IH_urban = 	u_inches_urban/u_lane_miles_urban if year>1992;
gen u_iri_IH_rural = 	u_inches_rural/u_lane_miles_rural  if year>1992;
drop u_inches_IH s_inches_IH ;

*---------------------------------------------------;
* - Figures (year) ;
*---------------------------------------------------;	


replace unionization=. if year<1983;
replace unionization_total=. if year<1983;


label var exp_L_IH_SF12a_r		"Construction USD2010";
label var exp_IRI_IH_SF12a_r 	"Resurfacing USD2010";
label var exp_IH_mtn_SF12a_r 	"Maintenance USD2010";
label var u_lane_miles_IH 		"Lane Miles IH, HPMS Universe";
label var s_lane_miles_IH 		"Lane Miles IH, HPMS Sample";	
label var u_aadt_IH				"AADT IH, HPMS Universe";	
label var s_aadt_IH				"AADT IH, HPMS Sample";	
label var s_iri_IHnat			"IRI IH, HPMS Sample";
label var u_iri_IHnat			"IRI IH, HPMS Universe";  

replace u_aadt_IH=u_aadt_IH	/10000;
replace u_aadt_IH_urban=u_aadt_IH_urban	/10000;
replace u_aadt_IH_rural=u_aadt_IH_rural	/10000;
replace s_aadt_IH=s_aadt_IH/10000;
replace s_lane_miles_IH=s_lane_miles_IH/1000;
replace u_lane_miles_IH=u_lane_miles_IH/1000;

foreach var in lane_miles_IH iri_IHnat {;
twoway 	con  u_`var'  year,msize(*.5) lw(*2)
		ylabel(#3)
		`pdf_plot_settings'; 
graph export "${output}/figures/Figure1_`var'.pdf", replace;
};

# d ;
foreach var in  aadt_IH  {;
twoway 	con  u_`var'  year,msize(*.5) lw(*2)|| 
	    con  s_`var'  year, msize(*.5)  lw(*2)
		color(gs5) lwidth(thick) lpattern(dash) 
		,
		leg( label(1 "Universe") label(2 "Sample") col(1) ring(0) pos(10) size(*0.5))
		`pdf_plot_settings'
		ylabel(0.5(0.25)1,format(%3.2fc)); 
graph export "${output}/figures/Figure1_`var'.pdf", replace;
};



*---------------------------------------------------;
*expenditure graph;
*---------------------------------------------------;

# d ;

gen sh_row=exp_row_IH_all*100/exp_L_IH_SF12a;


gen exp1=exp_L_IH_SF12a_r;
gen exp2=exp1+ exp_IRI_IH_SF12a_r;
gen exp3=exp2+ exp_IH_mtn_SF12a_r;

gen prc_expL=exp_L_IH_SF12a_r*100/exp3;
gen prc_expIRI=exp_IRI_IH_SF12a_r*100/exp3;
gen prc_expMTN=exp_IH_mtn_SF12a_r*100/exp3;


gen exp3_alter=exp2+ exp_IH_mtn_SF12_r;

foreach x in exp1 exp2 exp3{;
	replace `x' =`x'/1000;
};

twoway 	area exp1 year, color(gs2)|| 
	rarea exp2 exp1 year, color(gs7)|| 
	rarea exp3 exp2 year, color(gs12)
	,  ysc(r(0 25))
	`pdf_plot_settings'
	ymtick(0(5)25)
	ylabel(0(10)25,format(%9.0fc))
	legend(label(1 "Construction") label(2 "Resurfacing") label(3 "Maintenance") pos(10) ring(0) on size(*.9)); 
graph export "${output}/figures/Figure1_IH_expenditure.pdf", replace;




*Revenue graphs ; 

replace rev_grand_total_mt_r=. if year<1994 ;
			   
foreach var in  rev_grand_total_mt_r rev_total_h_r rev_total_mt_r	rev_total_m_fuel_h_r	{;	   
	gen `var'_IH=	`var'*(s_vmt_IH 	/s_vmt_NHS)  ;
	gen `var'_IH_per_VMT=`var'_IH*10^6/s_vmt_IH   ;
	*NOTE WE MULTIPLE BY 10^6 to get the revenue in dollars and not thousands USD ;
};

*BEcause we do not have this data yet; 
drop   if  year==1997 | year==1998 |year>2008;
replace rev_grand_total_mt_r_IH=rev_grand_total_mt_r_IH/1000;
replace rev_total_m_fuel_h_r_IH=rev_total_m_fuel_h_r_IH/1000;

replace rev_grand_total_mt_r_IH_per=rev_grand_total_mt_r_IH_per*1000;
replace  rev_total_m_fuel_h_r_IH_per_VMT= rev_total_m_fuel_h_r_IH_per_VMT*1000;


*---------------------------------------------------;
* Table  ;
*----------------------------------------------------;
gen selected_years=cond(year==1984 |
						year==1992 |
						year==2008,1,0);

merge 1:1 year using  "../intermediate_data/sh_surface_type_data", nogen ;
keep if selected_years==1						;

keep year	
u_urban_share_IH
IH_share_urb
IH_share_grade 
IH_share_h2O
IH_share_elev IH_share_new
lm_sn_d
sh_surf3 sh_surf2 sh_surf1
unionization 
Surf_BituminousconcreteAv_ctn10 ;

foreach var in u_urban_share_IH IH_share_urb IH_share_h2O IH_share_new sh_surf3 sh_surf2 sh_surf1 {;
	replace `var'=`var'*100;
};
mkmat u_urban_share_IH
IH_share_urb
IH_share_grade 
IH_share_h2O
IH_share_elev IH_share_new
lm_sn_d
sh_surf3 sh_surf2 sh_surf1
unionization  , matrix(Table ) rowname(year)	; 
		
		
matrix table=Table'		;
matrix list table
;

		esttab matrix(table, fmt( %9.2fc)) using "${output}/tables/Table1_Trends_table.tex", replace
		nomtitles 
	substitute(  "u_urban_share_IH" "Urban (HPMS)"
"IH_share_urb" "Urban (NLCD)"
"IH_share_grade" "Grade (HPMS)"
"IH_share_h2O" "Water (NLCD)"
"IH_share_elev" "Elevation"
"IH_share_new" "\(\%\) New miles"
"lm_sn_d" "Structural Number"
 "sh_surf3" "\(\%\) Flexible "
"sh_surf2" "\(\%\) rigid"
 "sh_surf1" "\(\%\) composite"
"unionization"  "Unionization"
)
		nogaps  compress
		 		 title("Lane mile weighted means of network characteristics \label{tab:trends_table}")

			 prehead(\begin{table}[H]\centering
\caption{@title}
	\begin{tabular}{l*{@span}{c}}\hline
	) 

posthead(\hline)
	postfoot(
	\hline
	\end{tabular}
\end{table})   ;



log close;
exit;
